{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ca1d9278-d0f8-49f4-9652-82f80429958f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Error: Unable to load image.\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ WARN:0@2.051] global loadsave.cpp:241 findDecoder imread_('code1/03.png'): can't open/read file: check file path/integrity\n"
     ]
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "from IPython.display import Image\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "\n",
    "def detect_display(image_path):\n",
    "    # 读取图片\n",
    "    image = cv2.imread(image_path)\n",
    "    if image is None:\n",
    "        print(\"Error: Unable to load image.\")\n",
    "        return\n",
    "\n",
    "    # 显示原始图片\n",
    "    cv2.imshow('Original Image', image)\n",
    "    cv2.imwrite('step1_original_image.jpg', image)\n",
    "\n",
    "    # 转换到灰度图\n",
    "    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "    # 显示灰度图\n",
    "    cv2.imshow('Gray Image', gray)\n",
    "    cv2.imwrite('step2_gray_image.jpg', gray)\n",
    "\n",
    "    # 应用高斯模糊\n",
    "    blurred = cv2.GaussianBlur(gray, (5, 5), 0)\n",
    "\n",
    "    # 显示高斯模糊后的图片\n",
    "    cv2.imshow('Blurred Image', blurred)\n",
    "    cv2.imwrite('step3_blurred_image.jpg', blurred)\n",
    "\n",
    "    # 边缘检测\n",
    "    edged = cv2.Canny(blurred, 30, 150)\n",
    "\n",
    "    # 显示边缘检测后的图片\n",
    "    cv2.imshow('Edged Image', edged)\n",
    "    cv2.imwrite('step4_edged_image.jpg', edged)\n",
    "\n",
    "    # 寻找轮廓\n",
    "    contours, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
    "\n",
    "    # 显示轮廓图\n",
    "    contour_image = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 2)\n",
    "    cv2.imshow('Contours Image', contour_image)\n",
    "    cv2.imwrite('step5_contours_image.jpg', contour_image)\n",
    "\n",
    "    # 遍历轮廓，寻找矩形显示器\n",
    "    displays = []\n",
    "    for contour in contours:\n",
    "        # 计算轮廓的近似多边形\n",
    "        approx = cv2.approxPolyDP(contour, 0.02 * cv2.arcLength(contour, True), True)\n",
    "\n",
    "        # 如果轮廓是四边形，可能是显示器\n",
    "        if len(approx) == 4:\n",
    "            # 计算边界框\n",
    "            x, y, w, h = cv2.boundingRect(contour)\n",
    "\n",
    "            # 检查宽高比，过滤掉不合理的四边形\n",
    "            if 1.3 < w / h < 1.8:  # 显示器的宽高比在1.3到1.8之间\n",
    "                #cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)\n",
    "                #displays.append((x, y, w, h))\n",
    "                # 在图片上显示四个角的实际坐标\n",
    "                #cv2.putText(image, f\"({x}, {y})\", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)\n",
    "                #cv2.putText(image, f\"({x+w}, {y})\", (x+w, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)\n",
    "                #cv2.putText(image, f\"({x}, {y+h})\", (x, y+h-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)\n",
    "                #cv2.putText(image, f\"({x+w}, {y+h})\", (x+w, y+h-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)\n",
    "                # 在图片上显示四个角的坐标\n",
    "                for i in range(4):\n",
    "                    \n",
    "                    x1, y1 = approx[i][0]\n",
    "                    cv2.line(image, approx[i][0], approx[(i + 1) % 4][0], (0, 255, 0), 2)\n",
    "                    cv2.putText(image, f\"({int(x1)}, {int(y1)})\", (int(x1)+10, int(y1)+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)\n",
    "                    cv2.circle(image, (x1, y1), 5, (255, 0, 0), -1)\n",
    "                # 在四个角添加圆点\n",
    "                #cv2.circle(image, (x, y), 5, (255, 0, 0), -1)\n",
    "                #cv2.circle(image, (x + w, y), 5, (255, 0, 0), -1)\n",
    "                #cv2.circle(image, (x, y + h), 5, (255, 0, 0), -1)\n",
    "                #cv2.circle(image, (x + w, y + h), 5, (255, 0, 0), -1)\n",
    "\n",
    "    # 显示最终结果\n",
    "    cv2.imshow('Detected Displays', image)\n",
    "    cv2.imwrite('final_detected_displays.png', image)\n",
    "\n",
    "    # 输出显示器的坐标\n",
    "    print(\"Detected Displays Coordinates:\")\n",
    "    for display in displays:\n",
    "        print(f\"Top-left: ({display[0]}, {display[1]}), Bottom-right: ({display[0]+display[2]}, {display[1]+display[3]})\")\n",
    "\n",
    "    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
    "    # Display the image using Matplotlib\n",
    "    plt.imshow(image_rgb)\n",
    "    plt.axis('off')  # Hide the axis\n",
    "    plt.show()\n",
    "    \n",
    "    # 等待按键，然后关闭所有窗口\n",
    "    cv2.waitKey(0)\n",
    "    cv2.destroyAllWindows()\n",
    "\n",
    "# 使用图片路径替换'path_to_image.jpg'\n",
    "detect_display('code1/03.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "570a93d2-5eba-4cd0-8d44-6159b5fe259c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
